﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.QuanHeHocPhanService;
namespace eTMS.Controllers{
public class QuanHeHocPhanControllerBase : CommonController{
	public QuanHeHocPhanControllerBase()
		: base()
	{	
		this.dao = new QuanHeHocPhanServiceClient();
	}
	~QuanHeHocPhanControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private QuanHeHocPhanEntity oQuanHeHocPhan;	
	public QuanHeHocPhanEntity QuanHeHocPhanObject{
		get{
			return oQuanHeHocPhan;
		}
		set{
			oQuanHeHocPhan=value;
		}
	}
	protected QuanHeHocPhanServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSQuanHeHocPhan(false);			
			return _Table;
		}
	}
	public void LoadAllDSQuanHeHocPhan(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oQuanHeHocPhan==null)return false;
		bool result=true;
		try{
			oQuanHeHocPhan = dao.SaveRet(oQuanHeHocPhan);
		}
		catch{
			result=false;
		}
		if(result){
			_ListQuanHeHocPhan.Add(oQuanHeHocPhan);
		}
		return result;
	}
	public bool Update(){
		if(oQuanHeHocPhan==null)return false;
		return dao.Update(oQuanHeHocPhan);
	}
	public bool Delete(){
		if(oQuanHeHocPhan==null)return false;
		bool retvalue = dao.Delete(oQuanHeHocPhan);
		if(retvalue){			
			_ListQuanHeHocPhan.Remove(oQuanHeHocPhan);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 QuanHeHocPhan_ID){
		bool retvalue = dao.DeleteByID(QuanHeHocPhan_ID);		
		if(retvalue){
			QuanHeHocPhanEntity oDeleted=FindQuanHeHocPhan(QuanHeHocPhan_ID);
			if(oDeleted!=null){
				_ListQuanHeHocPhan.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oQuanHeHocPhan = new QuanHeHocPhanEntity();
	}
    //Copy data from data row in grid to object
    public QuanHeHocPhanEntity MapDataRowToObject(DataRow row){
		QuanHeHocPhanEntity o = new QuanHeHocPhanEntity();
		 if(!row.IsNull("LoaiQuanHe"))
			o.LoaiQuanHe=ConvertByte(row["LoaiQuanHe"]);
		 if(!row.IsNull("TuChiTietChuongTrinh_ID"))
			o.TuChiTietChuongTrinh_ID=(Int32)(row["TuChiTietChuongTrinh_ID"]);
		 if(!row.IsNull("DenChiTietChuongTrinh_ID"))
			o.DenChiTietChuongTrinh_ID=(Int32)(row["DenChiTietChuongTrinh_ID"]);
		 if(!row.IsNull("QuanHeHocPhan_ID"))
		    o.QuanHeHocPhan_ID=(Int32)(row["QuanHeHocPhan_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(QuanHeHocPhanEntity o,DataRow row){
		row["LoaiQuanHe"]=o.LoaiQuanHe;
		row["TuChiTietChuongTrinh_ID"]=o.TuChiTietChuongTrinh_ID;
		row["DenChiTietChuongTrinh_ID"]=o.DenChiTietChuongTrinh_ID;
		row["QuanHeHocPhan_ID"]=o.QuanHeHocPhan_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.QuanHeHocPhanObject,row);
    }
	public void LoadObject(Int32 QuanHeHocPhan_ID){
		QuanHeHocPhanEntity oLoad=FindQuanHeHocPhan(QuanHeHocPhan_ID);
		if(oLoad==null){
			oQuanHeHocPhan=this.LoadQuanHeHocPhan(QuanHeHocPhan_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oQuanHeHocPhan.IsNullQuanHeHocPhan_ID;
			if(!bNotIsNull){
				this.ListQuanHeHocPhan.Add(oQuanHeHocPhan);
			}
		}
		else{
			oQuanHeHocPhan=oLoad;
		}
	}
	#region CachedList QuanHeHocPhan
	private static List<QuanHeHocPhanEntity> _ListQuanHeHocPhan=new List<QuanHeHocPhanEntity>();
    public List<QuanHeHocPhanEntity>ListQuanHeHocPhan{
		get{
			return _ListQuanHeHocPhan;
		}
	}
	public static List<QuanHeHocPhanEntity>ListQuanHeHocPhanStatic{
		get{
			return _ListQuanHeHocPhan;
		}
	}
	public QuanHeHocPhanEntity FindQuanHeHocPhan(Int32 QuanHeHocPhan_ID){
		QuanHeHocPhanEntity oResult=this.ListQuanHeHocPhan.Find(delegate(QuanHeHocPhanEntity oFind){
			bool result=true; 
			result&=oFind.QuanHeHocPhan_ID.Equals(QuanHeHocPhan_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllQuanHeHocPhan(bool clearCache){
		if(_ListQuanHeHocPhan.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		QuanHeHocPhanServiceClient dao = new QuanHeHocPhanServiceClient();
		_ListQuanHeHocPhan=new List<QuanHeHocPhanEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public QuanHeHocPhanEntity LoadQuanHeHocPhan(Int32 QuanHeHocPhan_ID){			
		QuanHeHocPhanEntity o=dao.SelectOne(QuanHeHocPhan_ID);		
		return o;
	}
	#endregion
}
}
